在Android Studio 3.x版,要用Google Map的話,可以使用最新的Google Maps Android API。除了必須建立Google Maps Activity的專案外,還必須要申請Google Maps的API Key。這樣才能使用此Google Map。
因為Google Map方面的程式可以應用的範圍很多,除了顯示地圖位置、地標之外還能切換不同的顯示模式。也可以依不同的需求,而有很多好玩的應用。
首先,請在Android Studio 3.x版,新建一個專案。增加一個Google Maps Activity。

產生出專案後,可以在res\values\google_maps_api.xml找到此檔案,此檔案,就是可以產生出API驗証的API Key。

在google_maps_api.xml裡面的內容,可以找到一個網址,將此網址放到瀏覽器中,來申請對應的API Key。

如果已經登入Google,就可以按「繼續」按鈕,就產生出API Key

產生API Key。


要產生出來的API Key,貼到下述的畫面,就可以執行了。

產生出API Key,系統會自動產生出下述的程式碼,基本上,就可以執行了。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
後續,我們再來看看可以有什麼變化,以及在實體機的情況。